/* 
  Soap-Service class which is responsible for the SOAP-webservice requests.
  Due to the Same-Origin-Policy of most modern Browsers which prevent AJAX-requests from a website to other domains,
  this Soap-Service prepares the requests and delegates them to a request-handler instead of directly calling the
  webservice.
*/

/*
* constructor:
* @param requestHandler uses the provided url to delegate the SOAP-requests to
* @param requestType request-type to be used
* */
function SoapService(requestHandler, requestType){
    this.requestHandler = requestHandler;
    this.requestType = requestType;
}

/*
* Helper-method to delegate SOAP-requests via AJAX (JQuery) to the SOAP-request-handler.
* @param data array which holds all key-value-data-pairs used for the soap-request (value to the key 'message' holds the name of the SOAP-request)
* */
SoapService.prototype.sendRequestToSoapRequestHandler = function(data) {
    var returnValue = null
    $.ajax({
        url: this.requestHandler,
        data: data,
        type: this.requestType,
        async: false,
        success: function(data) {
            returnValue = data;
        },
        error: function() {
            var soapRequestErrorHandler = new SoapRequestErrorHandler();
            soapRequestErrorHandler.warn("Keine Daten konnten vom SOAP-Webservice ermittelt werden!");
            returnValue = null; // PersistenceLayer interprets null as a problem within the SOAP-request.
        }
    });
    return returnValue;
}


/* Get all matches between the two datetime instances (fromDateTime <= matchBeginDateTime < toDateTime).
* @param fromDateTime start datetime
* @param toDateTime end datetime
* */
SoapService.prototype.getMatchesBetween = function(fromDateTime, toDateTime) {
    var data = {
        message: 'GetMatchdataByLeagueDateTime',
        fromDateTime: fromDateTime,
        toDateTime: toDateTime
    }
    
    return this.sendRequestToSoapRequestHandler(data);
}

/*
 * Get all matchdata for this group.
 * @param groupOrderID type of the group (e.g. Viertelfinale, Halbfinale, ...)
 * */
SoapService.prototype.getMatchdataForGroupType = function(groupOrderID) {
    var data = {
        message: 'GetMatchdataByGroupLeagueSaison',
        groupOrderID: groupOrderID
    }

    return this.sendRequestToSoapRequestHandler(data);
}

/*
 * Get match by matchID.
 * @param matchID Id of the match
 * */
SoapService.prototype.getMatch = function(matchID) {
    var data = {
        message: 'GetMatchByMatchID',
        matchID: matchID
    }

    return this.sendRequestToSoapRequestHandler(data);
}

/*
 * Get all Groups.
 * */
SoapService.prototype.getAllGroups = function() {
    var data = {
        message: 'GetAvailGroups'
    }
    return this.sendRequestToSoapRequestHandler(data);
}

